// DecodeWays.cpp : Defines the entry point for the console application.
//
/*
 A message containing letters from A-Z is being encoded to numbers using the following mapping:

'A' -> 1
'B' -> 2
...
'Z' -> 26

Given an encoded message containing digits, determine the total number of ways to decode it.

For example,
Given encoded message "12", it could be decoded as "AB" (1 2) or "L" (12).

The number of ways decoding "12" is 2. 
*/

#include "stdafx.h"
#include <string>
using namespace std;
class Solution 
{
public:



	int numDecodings(string s) 
	{
		int n = s.size();
		if (n == 0)
			return 0;
		int* c = new int[n];
		c[0] = 1;
		for(int i = 1; i <= n; i++ ) {
			int c1 = 0;
			if (s[i-1] != '0')
				c1 = c[i-1];
			int c2 = 0;
			if (i >= 2 && (s[i-2] == '1' || s[i-2] == '2' && s[i-1] <='6'))
				c2 = c[i-2];
			c[i] = c1 + c2;
		}
		int ret = c[n];
		delete [] c;

		return ret;
	}


};

int _tmain(int argc, _TCHAR* argv[])
{
	return 0;
}

